﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;
namespace BestLifePOS_PresentationLayer
{
    public partial class frmPriceListUpdate : Form
    {
        public frmPriceListUpdate()
        {
            InitializeComponent();
        }

        private void frmPriceListUpdate_Load(object sender, EventArgs e)
        {

        }

        private void RecallPriceList()
        {

            //Fill the DataGrid
            Recipt_Detail LvPL = new Recipt_Detail();
            //LvPR.status = "APPROVED";
            List<Price_List> LvList = Price_ListDAO.GetPrice_ListbyItemID(txtProductId.Text.ToString());


            DataTable LvTable = new DataTable();
            dgvPLDetails.DataSource = LvTable;

            LvTable.Columns.Add("PL_ID ");
            LvTable.Columns.Add("Pro_Price ");
            LvTable.Columns.Add("Ret_Price ");
            LvTable.Columns.Add("Pur_Price ");
            LvTable.Columns.Add("Pv ");
            LvTable.Columns.Add("Bv ");
            LvTable.Columns.Add("Exp_Date ");


            foreach (Price_List LvPL2 in LvList)
            {
                LvTable = (DataTable)dgvPLDetails.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPL2.PL_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvPL2.Pro_Price.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvPL2.Ret_Price.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvPL2.Pur_price.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvPL2.PV.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][5] = LvPL2.BV.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][6] = LvPL2.Exp_Date.ToString();



            }
        }

        private void btnRecall_Click(object sender, EventArgs e)
        {
            RecallPriceList();
        }

        private void dgvPLDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtPlId.Text = dgvPLDetails.Rows[dgvPLDetails.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
            dtpExpirDate.Text = dgvPLDetails.Rows[dgvPLDetails.SelectedCells[0].RowIndex].Cells[6].Value.ToString();
        }

        private void btnCal_Click(object sender, EventArgs e)
        {
            if (txtNewRPrice != null)
            {
                //gbxpvbvdisprice.Visible = true;
                List<Price_List> LvPricelist = new List<Price_List>();
                LvPricelist = Price_ListDAO.CalculationItemDetails(Convert.ToDecimal(txtNewPPrice.Text), Convert.ToDecimal(txtNewRPrice.Text));
                foreach (Price_List item in LvPricelist)
                {
                    lblProductPrice.Text = item.Pro_Price.ToString();
                    lblPv.Text = item.PV.ToString();
                    lblBv.Text = item.BV.ToString();

                }

            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtProductId != null)
            {
                int result = UpdatePrice();
                if (result != 0)
                {
                    MessageBox.Show("NEW PRICE WAS SUCCESFULLY UPLOADED");
                }
            }
        }

        private int UpdatePrice()
        {
            Price_List LvItemPL = CollectPriceListFigures();
            //Product LvItem = CollectItemFigures();
            if (LvItemPL != null)
            {
                return Price_ListDAO.UpdateNewPrice(LvItemPL);
                //return 1;
            }
            else
            {
                return 0;
            }
        }

        private Price_List CollectPriceListFigures()
        {
            try
            {
                Price_List LvItemList = new Price_List();
                LvItemList.BV = Convert.ToDecimal(lblBv.Text);
                LvItemList.Date_Create = System.DateTime.Now;
                LvItemList.Date_Modify = System.DateTime.Now;
                LvItemList.Exp_Date = Convert.ToDateTime(dtpExpirDate.Value);
                LvItemList.PL_Id = txtPlId.Text.ToString();//Price_ListDAO.GetNewPriceListID();
                LvItemList.Pro_ID = txtProductId.Text;
                LvItemList.Pro_Price = Convert.ToDecimal(lblProductPrice.Text);
                LvItemList.Pur_price = Convert.ToDecimal(txtNewPPrice.Text);
                LvItemList.PV = Convert.ToDecimal(lblPv.Text);
                LvItemList.Ret_Price = Convert.ToDecimal(txtNewRPrice.Text);
                LvItemList.User_Id = "1";
                LvItemList.User_Modify = "1";
                return LvItemList;
            }
            catch (Exception)
            {
                return null;
            }

        }
    }
}
